package com.c2c.controller.user;


import com.c2c.Dto.objAndTime;
import com.c2c.converter.DataConverterUtil;
import com.c2c.handler.DateTypeHandler;
import com.c2c.po.*;
import com.c2c.po.enums.impl.ComplaintsState;
import com.c2c.service.ComplaintService;
import com.c2c.service.GoodsService;
import com.c2c.service.OrderService;
import org.apache.taglibs.standard.extra.spath.Step;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;

@Controller
@RequestMapping("/userSell")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @Resource
    private OrderService orderService;

    @Resource
    private ComplaintService complaintService;

    //发布商品
    @RequestMapping("/save")
    public ModelAndView newGoods(Goods goods, ModelAndView modelAndView, MultipartFile file, Integer goodsClassifyId ,HttpSession session) throws IOException {
        boolean flag = true;
        if (goods.getGoodsName() == null || goods.getGoodsName().length() == 0) {
            modelAndView.addObject("error_name", "商品名不能为空");
            flag = false;
        }
        if (goods.getGoodsPrice() <= 0) {
            modelAndView.addObject("error_name", "商品价格异常");
            flag = false;
        }
        if (goods.getGoodsCondition() == null) {
            modelAndView.addObject("error_name", "商品成色不能为空");
            flag = false;
        }
        if (flag) {
            //获取用户账号
            User userAccount = (User) session.getAttribute("userAccount");
            goods.setUser(userAccount);
            //保存图片
            String filename = file.getOriginalFilename();//这个是文件名称
            file.transferTo(new File("E:\\JSP\\Juice_C2C_Platform\\src\\main\\webapp\\images" + filename));
            goods.setGoodsImg("\\images" + filename);
            GoodsClassify goodsClassify = new GoodsClassify();
            goodsClassify.setGoodsCategoryId(goodsClassifyId);
            goods.setGoodsClassify(goodsClassify);
            //存入数据库
            goodsService.insertGoods(goods);
        }
        modelAndView.setViewName("redirect:/userSell/myGoods");
        return modelAndView;
    }

    //我的发布
    @RequestMapping("/myGoods")
    public ModelAndView myComplaint(ModelAndView modelAndView, HttpSession session) {
        User userAccount = (User) session.getAttribute("userAccount");
        //该用户卖的所有商品
        List<objAndTime<Goods>> objAndTimeList = new ArrayList<>();
        List<Goods> goodsList = goodsService.findByUserId(userAccount.getUserAccount());
        for (Goods goods : goodsList) {
            Date goodsShelfTime = goods.getGoodsShelfTime();
            DataConverterUtil dataConverterUtil = new DataConverterUtil();
            String convert = dataConverterUtil.convert(goodsShelfTime);
            objAndTime<Goods> objAndTime = new objAndTime<Goods>();
            objAndTime.setStartTime(convert);
            objAndTime.setObject(goods);
            objAndTimeList.add(objAndTime);
        }


        //获取以卖家生成的订单
        List<objAndTime<Orders>> objAndTimeList1 = new ArrayList<>();
        List<Orders> allOrderByGoodsId = orderService.findAllOrderByGoodsId(userAccount.getUserAccount());
        for (Orders orders : allOrderByGoodsId) {
            Date orderEndTime = orders.getOrderEndTime();
            Date orderRiseTime = orders.getOrderRiseTime();
            String EndTime = new DataConverterUtil().convert(orderEndTime);
            String RiseTime = new DataConverterUtil().convert(orderRiseTime);
            //查找对应订单商品名称
            Goods byId = goodsService.findById(orders.getGoodsId());
            orders.setGoods(byId);
            //封装
            objAndTime<Orders> objAndTime = new objAndTime<Orders>();
            objAndTime.setEndTime(EndTime);
            objAndTime.setStartTime(RiseTime);
            objAndTime.setObject(orders);
            objAndTimeList1.add(objAndTime);
        }

        //获取所有分类
        List<GoodsClassify> all = goodsService.findAll();

        modelAndView.setViewName("userSell");
        modelAndView.addObject("goodsList", objAndTimeList);
        modelAndView.addObject("allOrderByGoodsId", objAndTimeList1);
        modelAndView.addObject("all", all);
        return modelAndView;
    }

    //商品详情
    @RequestMapping("/MyGoods")
    public ModelAndView orderDetail(HttpSession session, ModelAndView modelAndView) {
        //该用户购买的所有商品
        User userAccount = (User) session.getAttribute("userAccount");

        List<objAndTime<Orders>> objAndTimeList = new ArrayList<>();
        List<Orders> allOrderByGoodsId = orderService.findAllOrderByUserAccountId(userAccount.getUserAccount());
        for (Orders orders : allOrderByGoodsId) {
            Date orderEndTime = orders.getOrderEndTime();
            Date orderRiseTime = orders.getOrderRiseTime();
            String EndTime = new DataConverterUtil().convert(orderEndTime);
            String RiseTime = new DataConverterUtil().convert(orderRiseTime);
            //查找对应订单商品名称
            Goods byId = goodsService.findById(orders.getGoodsId());
            orders.setGoods(byId);
            //封装
            objAndTime<Orders> objAndTime = new objAndTime<Orders>();
            objAndTime.setEndTime(EndTime);
            objAndTime.setStartTime(RiseTime);
            objAndTime.setObject(orders);
            objAndTimeList.add(objAndTime);
        }

        //该用户生成的所有投诉
        List<Complaint> complaints = complaintService.findComplaintByUserAccount(userAccount.getUserAccount());
        List<objAndTime<Complaint>> objAndTimes = new ArrayList<>();
        for (Complaint complaint : complaints) {
            String EndTime = new DataConverterUtil().convert(complaint.getComplaintTime());
            objAndTime<Complaint> objAndTime = new objAndTime<Complaint>();
            objAndTime.setEndTime(EndTime);
            objAndTime.setObject(complaint);
            objAndTimes.add(objAndTime);
        }

        modelAndView.setViewName("userBuy");
        modelAndView.addObject("goodsList", objAndTimeList);
        modelAndView.addObject("complaints", objAndTimes);
        return modelAndView;
    }

    //修改
    @RequestMapping("/goodsUpdate")
    public String goodsUpdate(Goods goods, int goodsState, HttpSession session) {
        boolean error = false;
        int userAccount = (int) session.getAttribute("userAccount");
        goodsService.updateStateById(goods, goodsState);
        return "redirect:/userSell";
    }

}
